﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QLSV
{
    public partial class ThongTinMonHoc : Form
    {
        public ThongTinMonHoc()
        {
            InitializeComponent();
        }

        private SqlConnection Con;
        private DataTable Dt = new DataTable("MonHoc");
        private DataTable DtMH = new DataTable("MonHoc");
        private SqlDataAdapter Da = new SqlDataAdapter();

        private void Connect()
        {
            String Cn = "Data Source=LENOVOY510\\SQLEXPRESS;Initial Catalog=QLSV;Integrated Security=True";
            try
            {
                Con = new SqlConnection(Cn);
                Con.Open();
            }
            catch (Exception Ex)
            {
                MessageBox.Show("Không Thể Kết Nối Đến Cơ Sở Dữ Liệu!", "Lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

        private void DisConnect()
        {
            Con.Close();
            Con.Dispose();
            Con = null;
        }

        private void GetData()
        {
            SqlCommand Com = new SqlCommand();
            Com.Connection = Con;
            Com.CommandType = CommandType.Text;
            Com.CommandText = "Select * from MonHoc";
            Da.SelectCommand = Com;
            Da.Fill(DtMH);

            Com.CommandType = CommandType.Text;
            Com.CommandText = @"Select MaMH as N'Mã Môn Học',
                                          TenMH as N'Tên Môn Học',
                                          SoTC as N'Số Tín Chỉ',
                                          SoTiet as N'Số Tiết',
                                          HocKy as N'Học Kỳ',
                                          CNganh as N'Chuyên Ngành'
                                          from MonHoc";
            Da.SelectCommand = Com;
            Da.Fill(Dt);
            dgvttmh.DataSource = Dt;

            dgvttmh.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvttmh.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvttmh.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvttmh.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvttmh.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvttmh.Columns[5].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }

        private void ThongTinMonHoc_Load(object sender, EventArgs e)
        {
            Connect();
            GetData();
            Binding();
        }

        private void Binding()
        {
            txtmm.DataBindings.Clear();
            txtmm.DataBindings.Add("Text", dgvttmh.DataSource, "Mã Môn Học");
            txttm.DataBindings.Clear();
            txttm.DataBindings.Add("Text", dgvttmh.DataSource, "Tên Môn Học");
            txtst.DataBindings.Clear();
            txtst.DataBindings.Add("Text", dgvttmh.DataSource, "Số Tiết");
            txttc.DataBindings.Clear();
            txttc.DataBindings.Add("Text", dgvttmh.DataSource, "Số Tín Chỉ");
            txthk.DataBindings.Clear();
            txthk.DataBindings.Add("Text", dgvttmh.DataSource, "Học Kỳ");
            txtcn.DataBindings.Clear();
            txtcn.DataBindings.Add("Text", dgvttmh.DataSource, "Chuyên Ngành");
        }

        private void clickthoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Có Muốn Thoát Không?", "Thoát!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Close();
                Dispose();
            }
        }
    }
}
